package cn.gemframe.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;

/**
 * 
 * <P></P>
 *
 * <P></P> 
 *
 * @author zysh
 * @date 2017年12月22日
 * 
 */
public class GemDateUtils {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(GemDateUtils.class);

	public static String dateToStr(Date date, String format) {
		String str = null;
		try {
			SimpleDateFormat sf = null;
			if (format == null || "".equals(format)) {
				sf = new SimpleDateFormat("yyyyMMddHHmmss");
			} else {
				sf = new SimpleDateFormat(format);
			}
			str = sf.format(date);
		} catch (Exception e) {
			LOGGER.error("日期转换异常", e);
		}
		return str;
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param str
	 * @return date
	 */
	public static Date strToDate(String str, String format) {
		Date date = null;
		try {
			SimpleDateFormat sf = null;
			if (format == null || "".equals(format)) {
				sf = new SimpleDateFormat("yyyyMMddHHmmss");
			} else {
				sf = new SimpleDateFormat(format);
			}
			date = sf.parse(str);
		} catch (Exception e) {
			LOGGER.error("日期转换异常", e);
		}
		return date;
	}
	

	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.HOUR_OF_DAY);
	}
	 
	
	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.MINUTE);
	}
	 
	public static int getSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.SECOND);
	}

	/**
	 * 获取当天的已过的分钟数
	 * @return
	 */
	public static int getTodayPassedMinutes(){
		Date now = new Date();
		int currentHour = getHour(now);
		int currentMinute = getMinute(now);
		return (currentHour*60 + currentMinute);
	}
	
	/**
	 * 获取当天的已过的秒数
	 * @return
	 */
	public static int getTodayPassedSeconds(){
		Date now = new Date();
		int currentHour = getHour(now);
		int currentMinute = getMinute(now);
		int currentSecond =getSecond(now);
		return (currentHour*3600 + currentMinute*60 + currentSecond);
	}
	/**
	 * unix时间转换为普通时间
	 * @param unix
	 * @return
	 */
	public static String toLocalTime(String unix) {
	    Long timestamp = Long.parseLong(unix) * 1000;
	    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
	    return date;
	}
	 
	/**
	 * 普通时间转换为unix时间
	 * @param local
	 * @return
	 */
	public static String toUnixTime(String local){
	    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String unix = "";
	    try {
	        unix = df.parse(local).getTime()/1000 + "";
	    } catch (ParseException e) {
	    	LOGGER.error("日期转换异常", e);
	    }
	    return unix;
	}
	
	/**
	 * 获取n天前的unix时间
	 * @param day
	 * @return
	 */
	public static String getDateBeforeUnixTime(int day){
		Date date = getDateBefore(new Date(), day);
		String targetDate = dateToStr(date, "yyyy-MM-dd") + " 23:59:59";
		return toUnixTime(targetDate);
	}
	
	 public static Date getDateBefore(Date d, int day) {      
         Calendar now = Calendar.getInstance();      
         now.setTime(d);      
         now.set(Calendar.DATE, now.get(Calendar.DATE) - day);      
         return now.getTime();      
     }     
       
     public static Date getDateAfter(Date d, int day) {      
        Calendar now = Calendar.getInstance();      
        now.setTime(d);      
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);      
        return now.getTime();      
     }
     
 	public static Date getEndOfDay(Date date) {
 		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
 		LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
 		return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
 	}
 	
 	public static Date getStartOfDay(Date date) {
 		LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
 		LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
 		return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
 	}
 	
	public static String getCouponDateString(Date date){
		return getDateString(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	private static String getDateString(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}




	public static SimpleDateFormat ymdhms = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");


	public static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 将日期型转换为yyyy-MM-dd HH:mm:ss式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String getSimpleStringDate(Date date) {
		return ymdhms.format(date);
	}

	/**
	 * 将字符串解析成java.util.Date
	 *
	 * @param strDate
	 * @param pattern
	 * @return
	 */
	public static Date toDate(String strDate, String pattern) {
		Date reVal = null;
		DateFormat df = new SimpleDateFormat(pattern);
		try {
			reVal = df.parse(strDate);
		} catch (ParseException e) {
			reVal = null;
			e.printStackTrace();
		}
		return reVal;
	}

	/**
	 * 基本功能:获得某个时间段的月数
	 * <p/>
	 *
	 * @param startDate
	 * @param endDate
	 * @return int
	 */
	public static int getDaysMonth(Date startDate, Date endDate) {
		int end = getMonth(endDate);
		int start = getMonth(startDate);
		int months = 0;
		if (end < start) {
			months = end + 12 - start;
		} else {
			months = end - start;
		}
		return months;
	}

	/**
	 * 基本功能:得到月份，行如：MM
	 * <p/>
	 *
	 * @param date
	 * @return String
	 */
	public static int getMonth(Date date) {
		DateFormat df = new SimpleDateFormat("MM");
		return Integer.parseInt(df.format(date));
	}

	/**
	 * 基本功能:返回本月最后一天
	 * <p/>
	 *
	 * @param date
	 * @return Date
	 */
	public static Date getDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		return cal.getTime();
	}

	/**
	 * 基本功能:获得某个月的最后一天
	 * <p/>
	 *
	 * @param date
	 * @return int
	 */
	public static int getLastMonthDay(Date date) {
		int lastmonth;
		int lastmonthday = 0;
		int year = Integer.parseInt(getYear(date));
		try {
			lastmonth = getMonth(date);
			if (lastmonth == 0) {
				lastmonth = 12;
				year = year - 1;
			}
			switch (lastmonth) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					lastmonthday = 31;
					break;
				case 4:
				case 6:
				case 9:
				case 11:
					lastmonthday = 30;
					break;
				case 2:
					if ((year % 4) == 0)
						lastmonthday = 29;
					else
						lastmonthday = 28;
					break;
			}

		} catch (Exception e) {
			return 0;
		}
		return lastmonthday;
	}

	/**
	 * 基本功能:得到日期，行如：dd
	 * <p/>
	 *
	 * @param date
	 * @return String
	 */
	public static int getOneDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);

	}

	/**
	 * 返回当前时间
	 *
	 * @param pattern
	 * @return
	 */
	public static String getCurrentDate(String pattern) {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(pattern);
		String currentdate = "";
		try {
			currentdate = df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return currentdate;
	}

	/**
	 * 基本功能:转换成SQL.date类型
	 * <p/>
	 *
	 * @param strDate
	 * @param pattern
	 * @return java.sql.Date
	 */
	public static java.sql.Date toSqlDate(String strDate, String pattern) {
		return new java.sql.Date(toDate(strDate, pattern).getTime());
	}

	/**
	 * 将日期类转化成字符串，格式为"yyyy-MM-dd".
	 *
	 * @param date
	 * @return
	 */
	public static String toNormalPatternString(Date date) {
		return ymd.format(date);
	}

	/**
	 * 基本功能：返回日期的字符串到秒
	 * <p/>
	 *
	 * @param time
	 * @return String
	 */
	public static String getTimeAll(long time) {

		SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy-MM-dd ' 'HH:mm:ss");

		return formatter.format(new Date(time));

	}

	/**
	 * 基本功能：返回指定格式日期字符串
	 * <p/>
	 *
	 * @param time
	 * @param format
	 * @return String
	 */
	public static String getFormatStr(String format, long time) {
		String formatTime = "";

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		formatTime = formatter.format(new Date(time));

		return formatTime;
	}

	/**
	 * 基本功能：返回格林威治时间（1970-1-1）到指定日期的毫秒数
	 * <p/>
	 *
	 * @param format
	 * @param datestr
	 * @return long
	 * @throws Exception
	 */
	public static long getLongFormatDate(String format, String datestr) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		// ParsePosition pos = new ParsePosition(0);
		Date day;
		try {
			day = formatter.parse(datestr);
			return day.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}


	/**
	 * 计算两个日期之间相差的天数
	 * @param smdate 较小的时间
	 * @param bdate  较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate,Date bdate)
	{
		try {
			smdate=ymd.parse(ymd.format(smdate));
			bdate=ymd.parse(ymd.format(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 基本功能：返回第一个时间与第二个时间相差的秒数
	 * <p/>
	 *
	 * @param first
	 * @param second
	 * @return long
	 * @throws Exception
	 */
	public static long getDistance(Date first, Date second) {
		return (first.getTime() - second.getTime()) / 1000;
	}

	/**
	 * 获得24小时制long型时间，形如:2007-07-01 01:23 PM
	 *
	 * @param datestr
	 * @return
	 */
	public static long getLongTime24(String datestr) {
		long long24time = 0;
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd h:mm a", Locale.US);
			Date dd = df.parse(datestr);
			DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String ddstr = df1.format(dd);
			long24time = getLongFormatDate("yyyy-MM-dd HH:mm", ddstr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return long24time;
	}

	/**
	 * 基本功能：返回yyyy-MM-dd日期字符串
	 * <p/>
	 *
	 * @param time
	 * @return String
	 */
	public static String getTimeSimple(long time) {
		String formatTime = "";
		formatTime = ymd.format(new Date(time));

		return formatTime;
	}

	/**
	 * 基本功能：将long转换为Date
	 * <p/>
	 *
	 * @param time
	 * @return java.sql.Date
	 */
	public static java.sql.Date getDateTimeSimple(long time) {
		String formatTime = ymd.format(new Date(time));
		return java.sql.Date.valueOf(formatTime);
	}

	/**
	 * 基本功能：将yyyy-MM-dd格式字符串转换为long
	 * <p/>
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 *             long
	 */
	public static long getLongByTimeThreadStr(String time) throws Exception {
		ParsePosition pos = new ParsePosition(0);
		Date date = ymd.parse(time, pos);
		if (date == null)
			throw new Exception("the date/time string can not parse");
		return date.getTime();
	}

	/**
	 * 基本功能：将yyyy-MM-dd HH:mm:ss格式字符串转换为long
	 * <p/>
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 *             long
	 */
	public static long getLongByFullTime(String time) throws Exception {
		ParsePosition pos = new ParsePosition(0);
		Date date = ymdhms.parse(time, pos);
		if (date == null)
			throw new Exception("the date/time string can not parse");
		return date.getTime();
	}

	/**
	 * 基本功能：将yyyy.MM.dd格式字符串转换为long
	 * <p/>
	 *
	 * @param time
	 * @param type
	 * @return
	 * @throws Exception
	 *             long
	 */
	public static long getLongByTimeDotStrTime(String time, int type)
			throws Exception {
		long formatTime = 0;

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
		ParsePosition pos = new ParsePosition(0);
		Date date = formatter.parse(time, pos);
		if (date == null)
			throw new Exception("the date/time string can not parse");
		formatTime = date.getTime();

		return formatTime;
	}

	/**
	 * 基本功能：将java日期类转化成sql Timestamp类型
	 * <p/>
	 *
	 * @param date
	 * @return java.sql.Timestamp
	 */
	public static java.sql.Timestamp toSqlTimestamp(Date date) {
		if (date == null)
			return null;
		else
			return new java.sql.Timestamp(date.getTime());
	}

	/**
	 * 基本功能:字符串转换为Timestamp
	 * <p/>
	 *
	 * @param str
	 * @return java.sql.Timestamp
	 */
	public static java.sql.Timestamp toTimestamp(String str) {
		if (!str.equals("")) {
			Date date = toDate(str, "yyyy-MM-dd HH:mm:ss");
			return new java.sql.Timestamp(date.getTime());
		} else {
			return null;
		}
	}

	/**
	 * 基本功能：得到年月，行如：yyyy-MM
	 * <p/>
	 *
	 * @param date
	 * @return String
	 */
	public static String getYearMonth(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM");
		return df.format(date);
	}

	/**
	 * 基本功能：得到年，行如：yyyy
	 * <p/>
	 *
	 * @param date
	 * @return String
	 */
	public static String getYear(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy");
		return df.format(date);
	}

	/**
	 * 基本功能：得到日期，行如：yyyy-MM-dd
	 * <p/>
	 *
	 * @param date
	 * @return String
	 */
	public static String getDay(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	/**
	 * 基本功能:将日期DATE转换为String
	 * <p/>
	 *
	 * @param date
	 * @param parteen
	 * @return String
	 */
	public static String dateToString(Date date, String parteen) {
		DateFormat df = new SimpleDateFormat(parteen);
		return df.format(date);
	}

	/**
	 * 基本功能：得到date所在周的第一天(精确到天)
	 * <p/>
	 *
	 * @param InputDate
	 * @return String
	 */
	public static String getWeekStartDay(Date InputDate) {
		Date firstDate = getWeekStartDayDate(InputDate);
		// 所在周开始日期
		String beginDate = new SimpleDateFormat("yyyy-MM-dd").format(firstDate.getTime());

		return beginDate;
	}

	/**
	 * 基本功能：得到date所在周的第一天
	 * <p/>
	 *
	 * @param InputDate
	 * @return String
	 */
	public static Date getWeekStartDayDate(Date InputDate) {

		Calendar cDate = Calendar.getInstance();
		cDate.setFirstDayOfWeek(Calendar.MONDAY);
		cDate.setTime(InputDate);

		Calendar firstDate = Calendar.getInstance();

		firstDate.setFirstDayOfWeek(Calendar.MONDAY);
		firstDate.setTime(InputDate);

		Calendar lastDate = Calendar.getInstance();
		lastDate.setFirstDayOfWeek(Calendar.MONDAY);
		lastDate.setTime(InputDate);

		if (cDate.get(Calendar.WEEK_OF_YEAR) == 1 && cDate.get(Calendar.MONTH) == 11) {
			firstDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR) + 1);
			lastDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR) + 1);
		}

		int typeNum = cDate.get(Calendar.WEEK_OF_YEAR);
		System.out.println(typeNum);

		firstDate.set(Calendar.WEEK_OF_YEAR, typeNum);
		firstDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return firstDate.getTime();
	}

	/**
	 * 获取输入date所在周最后一天
	 * @param inputDate
	 * @return
	 * @throws ParseException
	 */
	public static String getWeekEndDay(Date inputDate) {
		Date lastDate = getWeekEndDayDate(inputDate);
		// 所在周结束日期
		String endDate = new SimpleDateFormat("yyyy-MM-dd").format(lastDate.getTime());
		return endDate;
	}
	/**
	 * 获取输入date所在周最后一天
	 * @param inputDate
	 * @return
	 * @throws ParseException
	 */
	public static Date getWeekEndDayDate(Date inputDate) {
		Calendar cDate = Calendar.getInstance();
		cDate.setFirstDayOfWeek(Calendar.MONDAY);
		cDate.setTime(inputDate);

		Calendar firstDate = Calendar.getInstance();

		firstDate.setFirstDayOfWeek(Calendar.MONDAY);
		firstDate.setTime(inputDate);

		Calendar lastDate = Calendar.getInstance();
		lastDate.setFirstDayOfWeek(Calendar.MONDAY);
		lastDate.setTime(inputDate);

		if (cDate.get(Calendar.WEEK_OF_YEAR) == 1 && cDate.get(Calendar.MONTH) == 11) {
			firstDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR) + 1);
			lastDate.set(Calendar.YEAR, cDate.get(Calendar.YEAR) + 1);
		}

		int typeNum = cDate.get(Calendar.WEEK_OF_YEAR);

		firstDate.set(Calendar.WEEK_OF_YEAR, typeNum);
		firstDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		// 所在周开始日期
		String beginDate = new SimpleDateFormat("yyyy/MM/dd").format(firstDate.getTime());

		lastDate.set(Calendar.WEEK_OF_YEAR, typeNum);
		lastDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return lastDate.getTime();
	}

	/**
	 * 获取上周的第一天
	 * @return
	 */
	public static Date getLastWeekFirstDayDate(){
		Date weekStartDate = GemDateUtils.getWeekStartDayDate(new Date());
		Calendar c = Calendar.getInstance();
		c.setTime(weekStartDate);
		c.add(Calendar.DAY_OF_WEEK , -1);
		return GemDateUtils.getWeekStartDayDate(c.getTime());
	}

	/**
	 * 获取上周的第一天(精确到天)
	 * @return
	 */
	public static String getLastWeekFirstDay(){
		return  GemDateUtils.getWeekStartDay(getLastWeekFirstDayDate());
	}

	/**
	 * 获取下周第一天日期
	 * @return
	 */
	public static Date getNextWeekFirstDayDate(){
		Calendar c = null;
		Date weekEndDate = GemDateUtils.getWeekEndDayDate(new Date());
		c = Calendar.getInstance();
		c.setTime(weekEndDate);
		c.add(Calendar.DAY_OF_WEEK , 1);
		return c.getTime();
	}

	/**
	 * 获取下周第一天（精确到天)
	 * @return
	 */
	public static String getNextWeekFirstDay(){
		Date nextWeekFirstDayDate = getNextWeekFirstDayDate();
		return new SimpleDateFormat("yyyy/MM/dd").format(nextWeekFirstDayDate);
	}

	/**
	 * 基本功能：得到date所在周的起始日期
	 * <p/>
	 *
	 * @param date
	 * @return String[]
	 */
	public static String[] getWeekStartEnd(Date date) {
		String[] str = new String[2];
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		calendar.add(Calendar.HOUR, -24 * (weekDay - 1));
		str[0] = getDay(calendar.getTime());

		calendar.setTime(date);
		calendar.add(Calendar.HOUR, 24 * (7 - weekDay));
		str[1] = getDay(calendar.getTime());
		return str;
	}

	/**
	 * 基本功能：得到date所在月的起始结束日期
	 * <p/>
	 *
	 * @param date
	 * @return String[]
	 */
	public static String[] getMonthStartEnd(Date date) {
		String[] str = new String[2];
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		str[0] = getDay(calendar.getTime());

		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		str[1] = getDay(calendar.getTime());
		return str;
	}

	public static void main(String[] args) {
		// System.out.println(DateFormatUtil.getLongTime24("2007-10-11 07:06
		// AM"));
		// System.out.println(DateFormatUtil.getTimeAll(2147483647));
		// long d = 1183220640000L;
		// System.out.println(DateFormatUtil.getTimeAll(new Long(d -
		// 259200000L)));

		// Date date = new Date();
		// String daaa = getWeekStartDay(date);
		// date = toDate(daaa, "yyyy-MM-dd");
		// String date = DateFormatUtil.getFormatStr("yyyy-MM-dd HH:mm:ss",
		// 1251432731296L);
		// System.out.println(date);
		System.out.println(getNextWeekFirstDay());
	}



	/**
	 * 转换日期格式为Date型
	 *
	 * @param date
	 * @return Date
	 */
	public static Date parseAnyDateToDate(String date) {
		if (null != date) {
			List<SimpleDateFormat> list = new ArrayList<SimpleDateFormat>();
			list.add(new SimpleDateFormat("yyyy年MM月dd日 HH:mm"));
			list.add(new SimpleDateFormat("yyyy年MM月dd日HH:mm"));
			list.add(new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"));
			list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
			list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
			for (SimpleDateFormat format : list) {
				try {
					Date newdate = format.parse(date);
					return newdate;
				} catch (Exception e) {
					continue;
				}
			}
		}
		return new Date();
	}

	/**
	 * 日期的月份增加或减少
	 *
	 * @param datestr
	 *            2005-09-09 ; len=5
	 * @param len
	 * @return 2006-02-09
	 */
	public static String addMonth(String datestr, int len) {
		StringTokenizer token = new StringTokenizer(datestr, "-");
		int year = Integer.parseInt(token.nextToken());
		int month = Integer.parseInt(token.nextToken());
		int day = Integer.parseInt(token.nextToken());
		Calendar date = Calendar.getInstance();
		month = month + len - 1;
		date.set(year, month, day);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date.getTime()).toString();
	}

	/**
	 * 按当前时间增加分钟
	 *
	 * @param datastr
	 * @param minute
	 * @return
	 */
	public static String addMinute(String datastr, int minute) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance(); // 当时的日期和时间
		String strtime = "";
		try {
			Date d = df.parse(datastr);
			c.setTime(d);
			long minutemillis = minute * 60 * 1000;
			c.setTimeInMillis(c.getTimeInMillis() + minutemillis);
			strtime = df.format(c.getTime());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return strtime;
	}

	/**
	 * 按当前时间增加小时
	 *
	 * @param datastr
	 * @param hour
	 * @return
	 */
	public static String addHour(String datastr, int hour) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance(); // 当时的日期和时间
		String strtime = "";
		try {
			Date d = df.parse(datastr);
			c.setTime(d);
			long hourmillis = hour * 60 * 60 * 1000L;
			c.setTimeInMillis(c.getTimeInMillis() + hourmillis);
			strtime = df.format(c.getTime());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return strtime;
	}

	/**
	 * 获取当前日期 yyyy-MM-dd HH:mm:ss
	 *
	 * @return String
	 */
	public static String getToday() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String mDateTime = formatter.format(cal.getTime());
		return mDateTime;
	}

	/**
	 * 获取当前日期 yyyy-MM-dd HH:mm:ss
	 *
	 * @return String
	 */
	public static String getDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(new Date());
	}

	/**
	 * 获取当前日期年 yyyy
	 *
	 * @return String
	 */
	public static String getCurrentYear() {
		String time = getToday();
		return time.substring(0, time.indexOf("-"));
	}

	/**
	 * 改变日期 在当前时间上加天数
	 * @param aDate   Date
	 * @param days  int （负数为减）
	 * @return String
	 */
	public static String addDateDay(Date aDate, int days) {
		String datestr = "";
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(aDate);
			cal.add(Calendar.DATE, days);
			aDate = cal.getTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			datestr = formatter.format(aDate);
		} catch (Exception ex) {
		}
		return datestr;
	}

	/**
	 * 改变时间  在当前时间上加天数
	 * @param aDate   Date
	 * @param days  int （负数为减）
	 * @return String
	 */
	public static Date addDateDayReturnDate(Date aDate, int days) {
		try {

			Calendar cal = Calendar.getInstance();
			cal.setTime(aDate);
			cal.add(Calendar.DATE, days);
			aDate = cal.getTime();
			return aDate;
		} catch (Exception ex) {
		}
		return null;
	}


	/**
	 * 改变时间  在当前时间上加天数,不包含时分秒
	 * @param aDate   Date
	 * @param days  int （负数为减）
	 * @return String
	 */
	public static Date addDateDaysReturnDateDay(Date aDate, int days) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date d = formatter.parse(formatter.format(aDate));
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			cal.add(Calendar.DATE, days);
			aDate = cal.getTime();
			return aDate;
		} catch (Exception ex) {
		}
		return null;
	}

	/**
	 * 改变日期 在当前时间上加天数
	 * @param aDate
	 * @param days
	 * @return
	 */
	public static String getChangeDate(String aDate, int days) {
		String datestr = "";
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date d = formatter.parse(aDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			cal.add(Calendar.DATE, days);
			d = cal.getTime();
			datestr = formatter.format(d);

		} catch (Exception ex) {

		}
		return datestr;
	}

	/**
	 * 改变日期时间      在当前时间上加天数
	 * @param aDate
	 * @param days
	 * @return
	 */
	public static String getChangeDateInfo(String aDate, int days) {
		String datestr = "";
		try {
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date d = formatter.parse(aDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			cal.add(Calendar.DATE, days);
			d = cal.getTime();
			datestr = formatter.format(d);

		} catch (Exception ex) {

		}
		return datestr;
	}

	/**
	 * 把当前日期格式转换成微秒格式
	 *
	 * @param aDate
	 * @return
	 */
	public static String getMillisTime(String aDate) {
		String datestr = "";
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = df.parse(aDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			datestr = datestr + cal.getTimeInMillis();
		} catch (Exception ex) {

		}
		return datestr;
	}

	/**
	 * 获取当前是周几
	 *
	 * @param dt
	 * @return
	 */
	public static int getWeekOfDate(Date dt) {
		int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 判断时间是不是今天
	 * @param date
	 * @return    是返回true，不是返回false
	 */
	public static boolean isCurrentDay(Date date) {
		//当前时间
		Date now = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		//获取今天的日期
		String nowDay = sf.format(now);
		//对比的时间
		String day = sf.format(date);

		return day.equals(nowDay);
	}

}
